package cn.stronglink.oil.map;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.stronglink.oil.R;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;

/**
 * Created by chen on 2017/10/19.
 */

public class HistoryInfoAdapter extends BaseAdapter {
    private List<DeviceRealTimeInfo.GasDevicesBean.GasInfosBean> gasInfoList = null;//气体列表
    private List<DeviceRealTimeInfo.GasDevicesBean> devicesBeanList = null;//设备列表
    private Context context;
    private int gasType = -1, deviceId = -1;
    private int count = 0, deviceCount = 0, gasCount = 0;

    public HistoryInfoAdapter(List<DeviceRealTimeInfo.GasDevicesBean> devicesBeanList, Context context) {
        this.devicesBeanList = devicesBeanList;
        this.context = context;
    }

    public void addRealTimeDiviceData(List<DeviceRealTimeInfo.GasDevicesBean> devicesBeanList) {
        if (this.devicesBeanList == null) {
            this.devicesBeanList = new ArrayList<>();
        }
        this.devicesBeanList.addAll(devicesBeanList);
        notifyDataSetChanged();
    }


    public void clearHistory() {
        if (devicesBeanList != null) {
            devicesBeanList.clear();
        }
        devicesBeanList = new ArrayList<>();
        deviceCount = 0;
        gasCount =0;
        count =0;
        notifyDataSetChanged();
    }

    public void setGasType(int gasType) {
        this.gasType = gasType;
    }

    public void setDeviceId(int deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public int getCount() {
        if (devicesBeanList != null && devicesBeanList.size() > 0) {
            deviceCount = devicesBeanList.size();
            gasInfoList = devicesBeanList.get(0).getGasInfos();
            if (gasInfoList != null && gasInfoList.size() > 0) {
                gasCount = devicesBeanList.get(0).getGasInfos().size();
            }
        }
        count = deviceCount * gasCount;
        return count;
    }

    @Override
    public Object getItem(int position) {
        int deviceIndex = -1, gasIndex = -1;
        Object object = null;
        deviceIndex = position / gasCount;
        gasIndex = position % gasCount;
        if (deviceIndex != -1 && gasIndex != -1) {
            object = devicesBeanList.get(deviceIndex).getGasInfos().get(gasIndex);
        }
        return object;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_history_info, parent, false);
            viewHolder.tv_gas_name = (TextView) convertView.findViewById(R.id.tv_gas_name);
            viewHolder.tv_gas_content = (TextView) convertView.findViewById(R.id.tv_gas_content);
            viewHolder.tv_gas_time = (TextView) convertView.findViewById(R.id.tv_gas_time);
            viewHolder.tv_gas_device_id = (TextView) convertView.findViewById(R.id.tv_gas_device_id);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        showHistoryData(position, viewHolder);
        return convertView;
    }

    private void showHistoryData(int position, ViewHolder viewHolder) {
        int deviceIndex = -1, gasIndex = -1;
        if (gasCount!=0) {
            deviceIndex = position / gasCount;
            gasIndex = position % gasCount;
            if (deviceIndex != -1 && gasIndex != -1) {
                DeviceRealTimeInfo.GasDevicesBean.GasInfosBean gasInfos = devicesBeanList.get(deviceIndex).getGasInfos().get(gasIndex);
                showHIstoryItemData(deviceIndex, viewHolder, gasInfos);
            }
        }

    }

    private void showHIstoryItemData(int deviceIndex, ViewHolder viewHolder, DeviceRealTimeInfo.GasDevicesBean.GasInfosBean gasInfos) {
        //显示所有历史数据
        viewHolder.tv_gas_name.setText(getGasName(gasInfos.getGasName()));
        viewHolder.tv_gas_content.setText(gasInfos.getGasVlaue() + "");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(devicesBeanList.get(deviceIndex).getGasTime());
        String dateStr = simpleDateFormat.format(date);
        viewHolder.tv_gas_time.setText(dateStr);
        viewHolder.tv_gas_device_id.setText(devicesBeanList.get(deviceIndex).getDeviceId() + "");
    }

    private String getGasName(String gasName) {
        String newGasName = "——";
        newGasName = TextUtils.isEmpty(gasName)?"——":gasName;
        newGasName = newGasName +":";
        return newGasName;
    }

    class ViewHolder {
        TextView tv_gas_name;
        TextView tv_gas_content;
        TextView tv_gas_time;
        TextView tv_gas_device_id;
    }
}
